package com.world.rickcloudendpoint.common.utils;

import com.neovisionaries.i18n.CountryCode;
import org.apache.tomcat.jni.Local;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @description: ios3 国家或地区编码处理工具
 * @author: jlsong
 * @date: 2022/04/08 08:04:21
 * @version: 1.0
 */
public class LocaleNationCodeUtils {

    private static Map<String, Locale> localeMap;

    public static void initNationCodeMap(){
        String[] countries = Locale.getISOCountries();
        localeMap = new HashMap<>(countries.length);
        for (String country: countries){
            Locale locale = new Locale("", country);
            localeMap.put(locale.getISO3Country().toUpperCase(), locale);
        }
    }


    public static String iso3CodeToIso2Code(String iso3Code){
        return localeMap.get(iso3Code).getCountry();
    }

    public static String iso2CodeToIso3Code(String iso2Code){
        Locale locale = new Locale("", iso2Code);
        return locale.getISO3Country();
    }

    public static void main(String[] args){
        LocaleNationCodeUtils.initNationCodeMap();
        // 显示国家或地区代码3字节--alpha-3
//        LocaleNationCodeUtils.localeMap.values().stream().forEach(r-> System.out.println(r.getISO3Country()));
        // 显示国家或地区代码2字节--alpha-2
//        LocaleNationCodeUtils.localeMap.values().stream().forEach(r-> System.out.println(r.getCountry()));
        // 显示国家或地区英文名
//        LocaleNationCodeUtils.localeMap.values().stream().forEach(r-> System.out.println(r.getDisplayCountry()));
        // 显示国家或地区中文名
//        LocaleNationCodeUtils.localeMap.values().stream().forEach(r-> System.out.println(r.getDisplayCountry(Locale.CHINA)));

//        CountryCode cc = CountryCode.getByCode("JP");
//        CountryCode cc = CountryCode.getByCode("CN");
//        System.out.println("Country name = " + cc.getName());                // "Japan"
//        System.out.println("ISO 3166-1 alpha-2 code = " + cc.getAlpha2());   // "JP"
//        System.out.println("ISO 3166-1 alpha-3 code = " + cc.getAlpha3());   // "JPN"
//        System.out.println("ISO 3166-1 numeric code = " + cc.getNumeric());  // 392
        for (CountryCode code : CountryCode.values()){
            System.out.println(code.getName());
        }
    }


}
